<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <MyHeader @addTodo="addTodo" />
        <MyList :todos="todos" />
        <MyFooter :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo" />
        <!-- <Kuayu>
          <template slot-scope="scopeData">
            <p
              v-for="(item,index) in scopeData.movies"
              :key=index
            >
              {{item}}
            </p>
          </template>
        </Kuayu> -->
        <router-link to="son" active-class="active">son</router-link>
        <br />
        <router-link to="kuayu" active-class="active">kuayu</router-link>
        <!-- 路由器的插槽 -->
        <router-view></router-view>
        <button @click="to_son">前往son</button>
        <button @click="back">后退</button>
        <button @click="forward">前进</button>
        请输入goNum<input type="text" v-model="goNum" />
        <button @click="go(goNum)">go</button>
        <el-row>
          <el-button>默认按钮</el-button>
          <el-button type="primary" @click='handlerAsyncList'>主要按钮，点击我，让异步组件显示或者隐藏！</el-button>
          <el-button type="success">成功按钮</el-button>
          <el-button type="info">信息按钮</el-button>
          <el-button type="warning"
            @click="changeBtnFlag">警告按钮</el-button>
            <el-button v-has="'create'">创建</el-button>
            <el-button v-has="'edit'">修改</el-button>
            <el-button v-has="'delete'">删除</el-button>
          <transition-group name="animate__animated animate__bounce"
            enter-active-class="animate__rubberBand"
            leave-active-class="animate__backOutLeft">
            <el-button type="danger"
              v-show="btnFlag"
              class="mybtn"
              :key="0">危险按钮1</el-button>
            <el-button type="danger"
              v-show="!btnFlag"
              class="mybtn"
              :key="1">危险按钮2</el-button>
          </transition-group>
        </el-row>
        <el-row>
          {{obj}}
        </el-row>
        <div v-if='isListShow'>
          <AsyncList />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import pubsub from "pubsub-js";
import MyHeader from "./components/MyHeader";
import MyList from "./components/MyList";
import MyFooter from "./components/MyFooter";
import Kuayu from "./components/Kuayu.vue";
import Loading from "./components/Loading.vue";
import Error from "./components/Error.vue";
import "animate.css";
import has from './directives/has'

export default {
  name: "App",
  components: {
    MyHeader,
    MyList,
    MyFooter,
    Kuayu,
    AsyncList: () => {
      // 规定浏览器控制台的资源叫什么名字
      import(/*webpackChunkName:'AsyncList2222222'*/ "./components/AsyncList.vue");
    },
  },
  data() {
    return {
      //由于todos是MyHeader组件和MyFooter组件都在使用，所以放在App中（状态提升）
      todos: JSON.parse(localStorage.getItem("todos")) || [],
      isListShow: false,
      goNum: 0,
      btnFlag: true,
      obj: {
        name: "carlos",
        age: 25,
        'big-num':55
      },
      arr: [1, 2, 3],
    };
  },
  created(){
    window.localStorage.setItem('permissions','["create","edit"]')
  },
  methods: {
    //添加一个todo
    addTodo(todoObj) {
      this.todos.unshift(todoObj);
    },
    //勾选or取消勾选一个todo
    checkTodo(id) {
      this.todos.forEach((todo) => {
        if (todo.id === id) todo.done = !todo.done;
      });
    },
    //更新一个todo
    updateTodo(id, title) {
      this.todos.forEach((todo) => {
        if (todo.id === id) todo.title = title;
      });
    },
    //删除一个todo
    deleteTodo(_, id) {
      this.todos = this.todos.filter((todo) => todo.id !== id);
    },
    //全选or取消全选
    checkAllTodo(done) {
      this.todos.forEach((todo) => {
        todo.done = done;
      });
    },
    //清除所有已经完成的todo
    clearAllTodo() {
      this.todos = this.todos.filter((todo) => {
        return !todo.done;
      });
    },
    to_son() {
      this.$router.push({
        path: "/son",
        // params query都可以在里面写
      });
    },
    forward() {
      this.$router.forward();
    },
    back() {
      this.$router.back();
    },
    go(n) {
      console.log("n", n);
      this.$router.go(n);
    },
    changeBtnFlag() {
      this.btnFlag = !this.btnFlag;
    },
    updateVue2() {
      console.log(this.obj.salary);
      // 视图不更新
      this.obj.salary = "3k";
      console.log(this.obj.salary);
    },
    updateVue22() {
      console.log(this.obj.salary);
      // this.obj.$set('salary','3k')
      // 这样更新就可以了哦！视图层也更新。下面两种方法二选一，一个是实例，一个是Vue原型上
      // this.$set(this.obj,'salary','3k')
      Vue.set(this.obj, "salary", "3k");
      console.log(this.obj.salary);
    },
    handlerAsyncList() {
      this.isListShow = !this.isListShow;
    },
  },
  directives:{
    has
  },
  watch: {
    todos: {
      deep: true,
      handler(value) {
        localStorage.setItem("todos", JSON.stringify(value));
      },
    },
  },
  mounted() {
    this.$bus.$on("checkTodo", this.checkTodo);
    this.$bus.$on("updateTodo", this.updateTodo);
    this.pubId = pubsub.subscribe("deleteTodo", this.deleteTodo);
    console.log('mounted');
  },
  beforeDestroy() {
    this.$bus.$off("checkTodo");
    this.$bus.$off("updateTodo");
    pubsub.unsubscribe(this.pubId);
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}
.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}
.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}
.btn-edit {
  color: #fff;
  background-color: skyblue;
  border: 1px solid rgb(103, 159, 180);
  margin-right: 5px;
}
.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn:focus {
  outline: none;
}
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
.active {
  color: #bd362f;
}
/* .mybtn {
  transition: .5s linear;
} */
/* 上面这个和下面二选一 */
</style>
